import { Injectable, NestInterceptor, CallHandler, ExecutionContext, HttpStatus } from '@nestjs/common';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { Validator, isObject, isArray } from 'class-validator';

interface Response<T> {
  data: T;
}
@Injectable()
export class ResponseInterceptor<T> implements NestInterceptor<T, Response<T>>{
  intercept(context: ExecutionContext, next: CallHandler<T>): Observable<Response<T>> {
    const validator = new Validator();
    const ctx = context.switchToHttp();
    const request = ctx.getRequest();
    console.log('进入全局响应拦截器。。。');
    return next.handle().pipe(
      map(data => {
        console.log('全局拦截器返回内容后。。。');
        return {
          code: HttpStatus.OK,
          timestamp: new Date().toISOString(),
          path: request.url,
          message: 'success',
          data
        };
      }),
    );
  }

}
